home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 4166 < prev    next >
Encoding:
Text File  |  1996-08-06  |  1.5 KB  |  53 lines

  1. Path: news1.h1.usa.pipeline.com!usenet
  2. From: grantp@usa.pipeline.com(Pete)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Newbie Q: Is there a Static pointer?
  5. Date: 28 Jan 1996 15:49:44 GMT
  6. Organization: Kalevi, Inc.
  7. Message-ID: <4eg5uo$67o@news1.usa.pipeline.com>
  8. NNTP-Posting-Host: pipe4.h1.usa.pipeline.com
  9. X-PipeUser: grantp
  10. X-PipeHub: usa.pipeline.com
  11. X-PipeGCOS: (Pete)
  12. X-Newsreader: Pipeline USA v3.3.0
  13.  
  14. On Jan 27, 1996 22:35:16 in article <Newbie Q: Is there a Static pointer?>,
  15. 'ua187@freenet.Victoria.BC.CA (Alex Walker)' wrote: 
  16.  
  17.  
  18. >    I'm learning C++ and at the moment making a doubly linked-list. 
  19. >    I'd like to have a pointer to an object of which there is only 1 
  20. >    copy and all memeber of the class (each node in the list) can use it.    
  21. >    This sounds like a static pointer, but does such an animal exists??. 
  22. >    I can't seem to find any info on anything like this, (typical for  
  23. >    a newbie eh?) 
  24. Yes.  Example: 
  25.  
  26. Header file: 
  27. class SomeObject; 
  28.  
  29. class Node 
  30.  { 
  31.    public: 
  32.      ... stuff 
  33.    static SomeObject * pSomeObject; 
  34.  }; 
  35.  
  36. In any source file: 
  37. SomeObject Node::pSomeObject = 0; 
  38.  
  39. Then, in your main(), before you start working with the list, 
  40. initialize the static member something like: 
  41.  
  42.   Node::pSomeObject = new SomeObject(....); 
  43.  
  44. Of course, if SomeObject is already created in global memory, just 
  45. give its address instead.  Also, pSomeObject member must be 
  46. accessible to main or you must provide a Set function for it. 
  47.  
  48. -- 
  49. Pete Grant 
  50. Kalevi, Inc. 
  51. Object Oriented Software Development
  52.